package com.hxzy.controller;

import com.hxzy.comm.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/5/24 16:35
 * @Version 1.0
 */
@Controller
public class PointerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 向消息队列=demo的中发送消息( 同步发送)
     * @param message
     * @return
     */
    @GetMapping(value = "/send/demo")
    public String  sendmessageToDemo(String message){

        //同步
        Object obj=this.rabbitTemplate.convertSendAndReceive(Constants.QUEUE_DEMO,message);
        System.out.println(obj);

        // 叫做视图名 前缀+视图名+后缀
        // templates/demo01.html
        return "demo01";
    }


    /**
     * 向消息队列=demo的中发送消息 (异步发送)
     * @param message
     * @return
     */
    @GetMapping(value = "/send/max/demo")
    public String  sendmessageMaxToDemo(String message){

        StopWatch  stopWatch=new StopWatch();
        stopWatch.start();
        for(int i=1;i<=100000;i++){
            //同步
            this.rabbitTemplate.convertAndSend(Constants.QUEUE_DEMO,message+"_"+i);

        }
        stopWatch.stop();
        System.out.println("1百万消息耗时："+stopWatch.getTotalTimeSeconds()+"毫秒");
        // 叫做视图名 前缀+视图名+后缀
        // templates/demo01.html
        return "demo01";
    }

}
